package org.javaboy.producer01.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayMsgConfig {

    public static final String DELAY_MSG_EXCHANGE = "delay_msg_exchange";
    public static final String DELAY_MSG_QUEUE = "delay_msg_queue";

    @Bean
    Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(DELAY_MSG_QUEUE).noargs();
    }

    @Bean
    CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        // 指定交换机类型，消息分发的特点，看这个参数
        args.put("x-delayed-type", "direct");
        CustomExchange customExchange = new CustomExchange(DELAY_MSG_EXCHANGE, "x-delayed-message", true, false,args);
        return customExchange;
    }

    @Bean
    Queue delayQueue() {
        return new Queue(DELAY_MSG_QUEUE, true, false, false);
    }
}
